/**
 * Created by mhy on 2018/8/7.
 */
import * as API from '../../api'
import * as types from '../mutation-types'
const material = {
  state: {
    frameList: [],
    frameCount: 0,
    detailFrame: {},

    matcardList: [],
    matcardCount: 0,
    detailMatcard:{},

    outFrameList: [],
    outFrameCount: 0,
    detailOutFrame: {},

    inFrameList: [],
    inFrameCount: 0,
    detailInFrame: {},

    outCardList: [],
    outCardCount: 0,
    detailOutCard: {},

    middleCardList: [],
    middleCardCount: 0,
    detailMiddleCard: {},

    inCardList: [],
    inCardCount: 0,
    detailInCard: {},


    mused:{
      outframe:[0,0],
      inframe:[0,0],
      outmatcard: [0,0],
      middlematcard: [0,0],
      inmatcard: [0,0],
      submanager: [0,0],
      subagent: [0,0]
    }
  },
  getters: {},
  mutations: {
    [types.SHOP_MATERIAL_LIST] (state, data){
      if(data.list && data.list.length > 0){
        let item = data.list[0]
        switch (item.mtype){
          case 'outframe':
            state.outFrameList  = state.outFrameList.concat(data.list)
            state.outFrameCount = data.total
            break
          case 'inframe':
            state.inFrameList  = state.inFrameList.concat(data.list)
            state.inFrameCount = data.total
            break
          case 'outmatcard':
            state.outCardList  = state.outCardList.concat(data.list)
            state.outCardCount = data.outCardCount
            break
          case 'middlematcard':
            state.middleCardList  = state.middleCardList.concat(data.list)
            state.middleCardCount = data.total
            break
          case 'inmatcard':
            state.inCardList  = state.inCardList.concat(data.list)
            state.inCardCount = data.total
            break
          default:
            break
        }
      }

    },
    [types.SHOP_MATERIAL_DETAIL] (state,data){
      let dataTemp = data[0]
      switch (dataTemp.mtype){
        case 'outframe':
          state.detailOutFrame = dataTemp
          break
        case 'inframe':
          state.detailInFrame = dataTemp
          break
        case 'outmatcard':
          state.detailOutCard = dataTemp
          break
        case 'middlematcard':
          state.detailMiddleCard = dataTemp
          break
        case 'inmatcard':
          state.detailInCard = dataTemp
          break
        default:
          break
      }
    },
    [types.FRAME_LIST] (state,data){
      state.frameList = data.list,
      state.frameCount = data.total
    },
    [types.FRAME_DETAIL] (state,data){
      state.detailFrame = data
    },
    [types.MATCARD_LIST] (state,data){
        state.matcardList = data.list,
        state.matcardCount = data.total
    },
    [types.MATCARD_DETAIL] (state,data){
      state.detailMatcard = data
    },
    [types.MATERIAL_USED] (state,data){
      state.mused = data
    },

  },
  actions: {
    getMaterialList({commit, state, rootState}, params){
      var prom = API.shopMaterial(params)
      prom.then(function (data) {
        commit(types.SHOP_MATERIAL_LIST, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getMaterialDetail({commit, state, rootState}, params){
      var prom = API.shopMaterial(params)
      prom.then(function (data) {
        commit(types.SHOP_MATERIAL_DETAIL, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    updateMaterial({commit, state, rootState}, params){
      var prom = API.shopMaterial(params)
      prom.then(function (data) {
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
    },
      getFrameList({commit, state, rootState}, params){
      var prom = API.infoFrame(params)
      prom.then(function (data) {
        commit(types.FRAME_LIST, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getFrameDetail({commit, state, rootState}, params){
      var prom = API.infoFrame(params)
      prom.then(function (data) {
        commit(types.FRAME_DETAIL, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getMatcardList({commit, state, rootState}, params){
      var prom = API.infoMatcard(params)
      prom.then(function (data) {
        commit(types.MATCARD_LIST, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getMatcardDetail({commit, state, rootState}, params){
      var prom = API.infoMatcard(params)
      prom.then(function (data) {
        commit(types.MATCARD_DETAIL, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    getMaterialUsed({commit, state, rootState}, params){
      var prom = API.materialCount(params)
      prom.then(function (data) {
        commit(types.MATERIAL_USED, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },

  }
}
export default material
